<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
  <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
  <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
  <!--<script src="http://{$Think.config.google_map_url}" async="async"></script>-->
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
  <style>
    .layui-upload-img{
      width: 100px;
    }
    .layui-table th{
      padding: 9px 6px;
    }
  </style>
</head>
<body>
<div style="margin-top: 20px;">
</div>

<form class="layui-form" id="admin">



  <div class="layui-form-item " id="">
    <label class="layui-form-label">优惠券名称</label>
    <div class="layui-input-inline" style="width: 500px">
      <input name="coupon_name"  value="{$edit.coupon_name}" placeholder="请输入优惠券名称" autocomplete="off" class="layui-input"  type="text">
    </div>
  </div>



  <div class="layui-form-item " >
    <label class="layui-form-label">已有商品</label>
    <div class="layui-input-inline" style="position: relative;z-index: 10000;width: 400px">
      <select  id="trip"  lay-filter="addtrips"   lay-verify="required" lay-search>
        <option value="999999">全部</option>
        {volist name="trip" id="vo"}
        <option value="{$vo.id}">{$vo.name}</option>
        {/volist}
      </select>
    </div>
    <span id="addtrips" data-name="全部" data-id="999999" class="layui-btn layui-btn-danger layui-btn-sm" style="margin-top: 4px">添加</span>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">已选商品</label>
    <div class="layui-input-inline" style="width: 500px" id="tripnames">
    <?php   $name= explode(',',$edit['name']);


            $id= explode(',',$edit['coupon_userange']);
        if (!empty($id[0])){
            $l=count($id);
            for ($i=0;$i<$l;$i++){
          ?>

      <span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="<?= $id[$i]?>" style="margin: 5px 10px"><?= $name[$i]?><i class="layui-icon"></i></span>
      <?php

      };

      }

?>



    </div>
  </div>
  <input name="coupon_userange" id="tripids"   type="hidden">


  <div class="layui-form-item">
    <label class="layui-form-label">选择类型</label>
    <div class="layui-input-inline">
      <input type="radio"  lay-filter="poitype" name="coupon_types" value="1" <?php if($edit['coupon_types']=='1'){ ?>checked<?php }?> title="折扣" >
      <input type="radio"  lay-filter="poitype" name="coupon_types" value="2" <?php if($edit['coupon_types']=='2'){ ?>checked<?php }?> title="满减" >
    </div>
  </div>

  <div class="layui-form-item " id="zhekou">
    <label class="layui-form-label">折扣</label>
    <div class="layui-input-inline" style="width: 500px">
      <input name="coupon_discount1"  value="{$edit.coupon_discount}" placeholder="请输入折扣 大于0 小于1 的小数 如1折=0.1" autocomplete="off" class="layui-input"  type="text">
    </div>
  </div>
  <div class="layui-form-item" style="display:none" id="manjian">
    <label class="layui-form-label">满减金额</label>
    <div class="layui-input-inline" style="width: 500px">
      <input name="coupon_discount2"  value="{$edit.coupon_discount}" placeholder="请输入满减金额" autocomplete="off" class="layui-input"  type="text">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">数量</label>
    <div class="layui-input-inline" style="position: relative;width: 500px">
      <input name="coupon_quantity"  value="{$edit.coupon_quantity}"   lay-verify="required" placeholder="请输入优惠券数量" autocomplete="off" class="layui-input" type="text">
      <div id="addressbox"></div>
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">使用条件</label>
    <div class="layui-input-inline" style="width: 500px">
      <div style="float: left">
        <input type="radio"  lay-filter="poitype" name="tiaojian" value="0"  <?php if($edit['coupon_usecondition']=='0'){ ?>checked<?php }?> title="不限制" >
        <input type="radio"  lay-filter="poitype" name="tiaojian" value="6"  <?php if($edit['coupon_usecondition']!='0'){ ?>checked<?php }?> title="满" >
      </div>
      <?php if($edit['coupon_usecondition']=='0'){ ?>
      <input name='mjje' id="mjje"  value='' disabled  placeholder='请输入金额' autocomplete='off' class='layui-input' style="width: 90px;float:left;margin: 0 10px 0 0; " type='text'><div style="height: 38px ; line-height: 38px">元可使用</div>
     <?php }else{

     ?>
      <input name='mjje' id="mjje"  value="<?= $edit['coupon_usecondition'] ?>"   placeholder='请输入金额' autocomplete='off' class='layui-input' style="width: 90px;float:left;margin: 0 10px 0 0; " type='text'><div style="height: 38px ; line-height: 38px">元可使用</div>


      <?php
     } ?>

    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">限制领取</label>
    <div class="layui-input-inline" style="position: relative;width: 500px">
      <input name="receive_limit"  value="{$edit.receive_limit}"   lay-verify="required" placeholder="请输入每个用户限制领取的数量" autocomplete="off" class="layui-input" type="text">

    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">领取时间</label>
    <div class="layui-input-inline" style="width: 500px">

      <div  class=" layui-col-xs5">
        <input name="etart_receivetime"  value="{$edit.etart_receivetime|date='Y-m-d',###}"  id="etart_receivetime"  lay-verify="required" placeholder="请输入开始时间" autocomplete="off" class="layui-input" type="text">
      </div>
      <div class="layui-col-xs2" style="text-align: center; line-height: 38px">~</div>
      <div  class=" layui-col-xs5">
        <input name="end_receivetime"  value="{$edit.end_receivetime|date='Y-m-d',###}"  id="end_receivetime"  lay-verify="required" placeholder="请输入结束时间" autocomplete="off" class="layui-input layui-col-xs5" type="text">
      </div> </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">使用时间</label>
    <div class="layui-input-inline" style="width: 500px">

      <div  class=" layui-col-xs5">
        <input name="etart_usetime"  value="{$edit.etart_usetime|date='Y-m-d',###}"  id="etart_usetime"  lay-verify="required" placeholder="请输入开始时间" autocomplete="off" class="layui-input" type="text">
      </div>
      <div class="layui-col-xs2" style="text-align: center; line-height: 38px">~</div>
      <div  class=" layui-col-xs5">
        <input name="end_usetime"  value="{$edit.end_usetime|date='Y-m-d',###}"  id="end_usetime"  lay-verify="required" placeholder="请输入结束时间" autocomplete="off" class="layui-input layui-col-xs5" type="text">
      </div> </div>


  </div>


  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>

</form>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>

<script>
    layui.use(['layer', 'form','laydate'], function() {
        // var laydate = layui.laydate;
        var layer = layui.layer,
            $ = layui.jquery,
            laydate= layui.laydate,
            form = layui.form;
        laydate.render({
            elem: '#etart_receivetime' //指定元素
        });
        laydate.render({
            elem: '#end_receivetime' //指定元素
        });
        laydate.render({
            elem: '#etart_usetime' //指定元素
        });
        laydate.render({
            elem: '#end_usetime' //指定元素
        });

        $(window).on('load', function() {

            form.on('radio(poitype)', function(data){
                var val= $(this).val();
                if (val=='1'){
                    $('#zhekou').show();
                    $('#manjian').hide();

                }else  if (val=='2') {
                    $('#manjian').show();
                    $('#zhekou').hide();

                }else  if (val=='0') {
                    $('#mjje').attr("disabled","disabled");
                    $('#mjje').val('')
                }else  if (val=='6') {
                    $("#mjje").removeAttr("disabled");
                }





            })

            //商品范围选择下拉添加事件
            form.on('select(addtrips)', function(data){
                var tripid=data.value;
                $("#addtrips").attr("data-id",tripid);
            });

            $("#addtrips").on("click",function () {
                var tripid= $("#addtrips").attr("data-id");
                var tripids=$("#tripids").val();
                var ids=tripids.split(",");

                if (tripid==999999){
                    tripids=999999;
                    var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="999999" style="margin: 5px 10px">全部<i class="layui-icon"></i></span>';
                    $("#tripnames").html(tripspan);
                }else{

                    for(var i=0 ; i < ids.length; i++){
                        if(tripid==ids[i]){
                            layer.msg("不能重复添加商品");
                            return false;
                        }
                        if(ids[i]==999999){
                            tripids=999999;
                            var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="999999" style="margin: 5px 10px">全部<i class="layui-icon"></i></span>';
                            $("#tripnames").html(tripspan);
                            $("#tripids").val(tripids);
                            return false;
                        }
                    }
                    var tripname="";
                    $("#trip").find("option").each(function () {
                        if($(this).val()==tripid){
                            tripname=$(this).html();
                        }
                    });

                    var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="'+tripid+'" style="margin: 5px 10px">'+tripname+'<i class="layui-icon"></i></span>';

                    if(!tripids){
                        tripids+=tripid;
                    }else{
                        tripids+=','+tripid;
                    }
                    $("#tripnames").append(tripspan);
                }



                $("#tripids").val(tripids);
            })


            $(document).on("click",'.deltripspan',function () {
                var dataid=$(this).attr("data-id");
                var tripids=$("#tripids").val();
                var ids=tripids.split(",");
                var newid="";
                for(var i=0 ; i < ids.length; i++){
                    if(ids[i]&&dataid!=ids[i]){
                        if(!newid){
                            newid+=ids[i];
                        }else{
                            newid+=","+ids[i];
                        }
                    }
                }
                $("#tripids").val(newid);
                $(this).remove();
            })



            form.on('submit(admin)', function(data) {

                $.ajax({
                    url:"{:url('admin/Coupon/addcoupon')}",
                    data:$('#admin').serialize(),
                    type:'post',
                    async: false,
                    success:function(res) {
                        layer.msg(res.msg);
                        if(res.code == 1) {
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },1500)
                        }
                    }
                })
                return false;
            });
        });
    });
</script>
<!-- 配置文件 -->
<script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('introduce');
    var ue2 = UE.getEditor('introduce2');
    var ue3 = UE.getEditor('introduce3');
    var ue4 = UE.getEditor('introduce4');
    var ue5 = UE.getEditor('introduce5');
</script>
</body>
</html>
